// const baseUrl = 'http://192.168.110.67:8088'; 
const baseUrl = 'http://api.inst-test.sunsetcare-mirrortech.com:32111'//测试
// const baseUrl = 'http://api.inst.sunsetcare-mirrortech.com'; //正式
// const baseUrl = '/dpc';
const httpRequest = (url, method, data, tokens)=>{
	let token = '';
	if (uni.getStorageSync('userInfo')) {
		token = 'Bearer '+uni.getStorageSync('userInfo');
	}
	if (tokens) {
		token = tokens;
	}
	let obj;
	switch (method) {
		case 'get':
			obj = {
				// 'X-Requested-With': 'XMLHttpRequest',
				"Accept": "application/json",
				"Content-Type": "application/json; charset=UTF-8",
				'Authorization': token
			};
			break;
		case 'post':
			obj = {
				'X-Requested-With': 'XMLHttpRequest',
				'Content-Type': 'application/json; charset=UTF-8',
				'Authorization': token
			}
			break;
		case 'posts':
			obj = {
				'X-Requested-With': 'XMLHttpRequest',
				'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
				'Authorization': token
			};
			method = 'post';
			break;
		case 'delete':
			obj = {
				'Custom-Header': 'delete',
				'Content-Type': 'application/json',
				'Authorization': token
			};
			break;
		case 'put':
			obj = {
				'Custom-Header': 'put',
				'Content-Type': 'application/json',
				'Authorization': token
			};
			break;
	}
	let fullUrl;
	if (url.indexOf("http") == 0) {
		fullUrl = url;
	} else {
		fullUrl = baseUrl + url;
	}
	let httpDefaultOpts = {
		url: fullUrl,
		data: data,
		method: method,
		header: obj,
		dataType: 'json',
	}
	// uni.showLoading({
	// 	title: '加载中'
	// })
	let promise = new Promise(function(resolve, reject) {
		uni.request(httpDefaultOpts).then(
			(res) => {
				uni.hideLoading();
				if (!res[1]) {
					uni.showToast({
						icon: 'none',
						title: '请求超时',
						duration: 2000
					})
					let obj = {
						data: {
							code: 100000
						}
					}
					resolve(obj)
				} else if (res[1].data.code != 200) {
					uni.showToast({
						icon: 'none',
						title: res[1].data.message,
					})
					if (res[1].data.code == 401) {
						uni.removeStorageSync('userInfo');
						let pages = getCurrentPages();
						let route = pages[pages.length - 1].route;
						let len = route.split('/').length;
						uni.navigateTo({
							url:'/pages/user/login'
						})
						switch (len) {
							case 3:
								uni.navigateTo({
									url: '/pages/user/login'
								});
								break;
							case 4:
								uni.navigateTo({
									url: '/pages/user/login'
								});
								break;
							case 5:
								uni.navigateTo({
									url: '/pages/user/login'
								});
								break;
						}
					}
					resolve(res[1])
				} else {
					resolve(res[1])
				}

			}
		).catch(
			(response) => {
				uni.hideLoading();
				reject(response)
			}
		)
	})
	return promise
};
export default {
	baseUrl,
	httpRequest
}
